<?php
//只包含form中间部分input的代码,不包含其他部分
//快速实现原型,先不考虑扩展性,先明确需求
//所有css和js需求需要预先加载在页面上.
class TbfFormView{
    static $FormViewTplPath;
    //生成很多表单的显示的html
    static function generate($field_array = array()){
        $output = '';
        foreach($field_array as $fieldInfo){
            $output .= self::generateOne($fieldInfo);
        }
        return $output;
    }
    //生成一条表单的显示的html
    static private function generateOne($fieldInfo){
        $type = $fieldInfo['Type'];
        if (!isset($fieldInfo['Value'])){
            $fieldInfo['Value'] = '';
        }
        if (empty($fieldInfo['ShowName'])&&(!empty($fieldInfo['Name']))){
            $fieldInfo['ShowName'] = $fieldInfo['Name'];
        }
        $cfuncName = 'Form'.$type;
        if (method_exists(__CLASS__,$cfuncName)){
            return self::$cfuncName($fieldInfo);
        }
        return self::viewFile('String',array(
            'ShowName'=>$fieldInfo['ShowName'].'(未实现:'.$type.')',
            'Name'=>$fieldInfo['Name'],
            'Value'=>$fieldInfo['Value'],
            'Info'=>$fieldInfo,
        ));
    }

    //字符串类型
    static function FormString($fieldInfo){
        return self::viewFile('String',array(
            'ShowName'=>$fieldInfo['ShowName'],
            'Name'=>$fieldInfo['Name'],
            'Value'=>$fieldInfo['Value'],
            'Info'=>$fieldInfo,
        ));
    }

    //密码类型
    static function FormPassword($fieldInfo){
        return self::viewFile('Password',array(
            'ShowName'=>$fieldInfo['ShowName'],
            'Name'=>$fieldInfo['Name'],
            'Info'=>$fieldInfo,
        ));
    }

    //隐藏
    static function FormHidden($fieldInfo){
        return self::viewFile('Hidden',array(
            'Name'=>$fieldInfo['Name'],
            'Value'=>$fieldInfo['Value'],
            'Info'=>$fieldInfo,
        ));
    }

    //下拉菜单 ,有很多key,value对的选项,只能选一个
    static function FormSelect($fieldInfo){
        return self::viewFile('Select',array(
            'ShowName'=>$fieldInfo['ShowName'],
            'Name'=>$fieldInfo['Name'],
            'OptionList'=>$fieldInfo['OptionList'],
            'Value'=>$fieldInfo['Value'],
            'Info'=>$fieldInfo,
        ));
    }

    //仅显示一条信息
    static function FormInfo($fieldInfo){
        return self::viewFile('Info',array(
            'ShowName'=>$fieldInfo['ShowName'],
            'Value'=>$fieldInfo['Value'],
            'Info'=>$fieldInfo,
        ));
    }

    static function FormHtml($fieldInfo){
        return self::viewFile('Html',array(
            'ShowName'=>$fieldInfo['ShowName'],
            'Name'=>$fieldInfo['Name'],
            'Value'=>$fieldInfo['Value'],
            'Info'=>$fieldInfo,
        ));
    }

    static function FormInt($fieldInfo){
        return self::viewFile('Int',array(
            'ShowName'=>$fieldInfo['ShowName'],
            'Name'=>$fieldInfo['Name'],
            'Value'=>$fieldInfo['Value'],
            'Info'=>$fieldInfo,
        ));
    }

    static function FormImage($fieldInfo){
        return self::viewFile('Image',array(
            'ShowName'=>$fieldInfo['ShowName'],
            'Name'=>$fieldInfo['Name'],
            'Value'=>$fieldInfo['Value'],
            'Info'=>$fieldInfo,
        ));
    }

    static function FormDateTime($fieldInfo){
        return self::viewFile('DateTime',array(
            'ShowName'=>$fieldInfo['ShowName'],
            'Name'=>$fieldInfo['Name'],
            'Value'=>$fieldInfo['Value'],
            'Info'=>$fieldInfo,
        ));
    }

    static function FormAttach($fieldInfo){
        return self::viewFile('Attach',array(
            'ShowName'=>$fieldInfo['ShowName'],
            'Name'=>$fieldInfo['Name'],
            'Value'=>$fieldInfo['Value'],
            'Info'=>$fieldInfo,
        ));
    }

    //该组件一个页面上只能使用一个
    static function FormSelect2Level($fieldInfo){
        if (empty($fieldInfo['Field'][0]['Value'])){
            $fieldInfo['Field'][0]['Value'] = '';
        }
        if (empty($fieldInfo['Field'][1]['Value'])){
            $fieldInfo['Field'][1]['Value'] = '';
        }
        $fieldInfo['Field'][0]['OptionList'] = $fieldInfo['OptionList'];
        foreach($fieldInfo['OptionList'] as $option){
            if ($option['Name']==$fieldInfo['Field'][0]['Value']){
                $fieldInfo['Field'][1]['OptionList'] = $option['OptionList'];
                break;
            }
        }
        if (empty($fieldInfo['Field'][1]['OptionList'])){
            $fieldInfo['Field'][1]['OptionList'] = $fieldInfo['OptionList'][0]['OptionList'];
        }
        return self::viewFile('Select2Level',array(
            'Field'=>$fieldInfo['Field'],
            'OptionList'=>$fieldInfo['OptionList'],
        ));
    }

    //复选框
    static function FormMultipleSelect($fieldInfo){
        return self::viewFile('MultipleSelect',array(
            'ShowName'=>$fieldInfo['ShowName'],
            'Name'=>$fieldInfo['Name'],
            'OptionList'=>$fieldInfo['OptionList'],
            'Info'=>$fieldInfo,
        ));
    }

    static private function viewFile($__tplName,$d=array()){
        ob_start();
        require self::$FormViewTplPath.'/'.$__tplName.'.php';
        return ob_get_clean();
    }
}
